<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;


class LightsCard extends Base
{
    const REDIS_CONFIG_DATE = 'REDIS:CONFIG:DATE';//活动时间配置
    const CARD_POND = 'CARD:GIFT:POND';//点亮卡礼物池子
    const CARD_CLICK_MEDAL = 'CARD:CLICK:MEDAL';//是否可领取勋章
    const CARD_USER_AWARD_LOG = 'CARD:USER:AWARD:LOG';//用户参与活动获奖记录


    const award_gift_id = [
        20 => 4,
        277 => 3,
        267 => 3,
        237 => 3,
        255 => 3,
        371 => 3,
        71 => 3,
    ];//奖池礼物id
    const award_gift_type = [4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 1, 1, 2, 2];//奖池礼物类型 1 头饰 2座驾 3礼物 4点亮卡


    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    //生成奖池
    public function createPond()
    {
        $key = self::CARD_POND;
        $gift_ids = array_keys(self::award_gift_id);
        $num = [1, 1, 2, 4, 450, 1000, 8542];
        $pond_arr = [];
        foreach ($num as $k => $v) {
            $arr = array_fill(0, $v, $gift_ids[$k]);
            $pond_arr = array_merge($pond_arr, $arr);
        }
        shuffle($pond_arr);
        Redis::LPUSH($key, $pond_arr);
        return $this->ttlRedisKey($key);
    }

    public function getAllPond()
    {
        $key = self::CARD_POND;
        return Redis::lrange($key, 0, -1);
    }

    //出奖池
    public function popPond()
    {
        $key = self::CARD_POND;
        return Redis::LPOP($key);
    }

    //出奖池
    public function delPond()
    {
        $key = self::CARD_POND;
        return Redis::DEL($key);
    }

    /* 获取礼物type
     * @param $pop_id
     *
     * return $type 1 头饰 2座驾 3礼物 4道具卡
     * */
    public function checkGiftType($pop_id)
    {
        $gift_arr = self::award_gift_id;
        return $gift_arr[$pop_id];
    }

    //获取礼物优先级
    public function getGiftLevel($gift_id)
    {
        $gift_arr = array_keys(self::award_gift_id);
        $gift_arr = array_flip($gift_arr);
        return $gift_arr[$gift_id];
    }

    //领取勋章添加
    public function addMedalCache($user_id)
    {
        $key = self::CARD_CLICK_MEDAL;
        Redis::HSET($key, $user_id, 1);
        return $this->ttlRedisKey($key);
    }

    //领取勋章
    public function getMedalCache($user_id)
    {
        $key = self::CARD_CLICK_MEDAL;
        return Redis::HGET($key, $user_id);
    }

    public function createAwardLog($user_id, $gift_id, $type)
    {
        $key = self::CARD_USER_AWARD_LOG . '-' . $user_id;
        $k = $gift_id . '-' . time() . '-' . rand(1, 10000);
        Redis::HSET($key, $k, $type);
        return $this->ttlRedisKey($key);

    }

    public function getAwardLog($user_id)
    {
        $key = self::CARD_USER_AWARD_LOG . '-' . $user_id;
        return Redis::HGETALL($key);
    }


}
